package com.ternnetwork.commons.utils;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpProtocolParams;


/**
 * Http工具类
 * 
 * @author xufeng
 */
public class HttpClientUtils {
	
	
	public static void main(String[] args) {
		 String resp=get("https://www.mbet.com/zh/popular/Football/?menu=11");
		 System.out.println(resp); 
   }
	
	private static final String CHARSET = "UTF-8";
	private static CookieStore _cookieStore = null;

	/**
	 * 调用HTTP GET请求
	 * 
	 * @param url
	 * @param param
	 * @return String
	 * @throws Exception
	 */
	public static String get(String url) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(url);
		if (_cookieStore != null) {
			httpclient.setCookieStore(_cookieStore);
		}
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = null;
		try {
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (Exception e) {
			
		}
		_cookieStore = httpclient.getCookieStore();
		httpclient.getConnectionManager().shutdown();
		return responseBody;
	}

	/**
	 * 调用HTTP POST请求
	 * 
	 * @param url
	 * @param param
	 * @return String
	 * @throws Exception
	 */
	public static String post(String url, Map<String, String> param) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				HttpProtocolParams.HTTP_CONTENT_CHARSET, CHARSET);
		HttpPost httppost = new HttpPost(url);
		if (param != null && !param.isEmpty()) {
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			for (Map.Entry<String, String> entry : param.entrySet()) {
				nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry
						.getValue()));
			}
			try {
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
						CHARSET));
			} catch (UnsupportedEncodingException e) {
			}
		}
		if (_cookieStore != null) {
			httpclient.setCookieStore(_cookieStore);
		}
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = null;
		try {
			responseBody = httpclient.execute(httppost, responseHandler);
		} catch (Exception e) {
		
		}
		_cookieStore = httpclient.getCookieStore();
		httpclient.getConnectionManager().shutdown();
		return responseBody;
	}

	public String encode(String value) {
		String result = "";
		if (value != null) {
			try {
				result = URLEncoder.encode(value, CHARSET);
			} catch (Exception e) {
			}
		}
		return result;
	}
	
	public static InputStream getRemoteImg(String url)
	{
		 try {
		    HttpClient client = new org.apache.commons.httpclient.HttpClient();
	        client.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5"); 
			GetMethod get = new GetMethod(url);   
			get.getParams().setParameter("http.protocol.cookie-policy",CookiePolicy.BROWSER_COMPATIBILITY);
			client.executeMethod(get);   
			return get.getResponseBodyAsStream();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//获取输入流
	        
	        return null;
	}


}
